﻿package client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import javax.swing.*;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import client.*;
import javax.swing.JLabel;
import java.awt.Font;

import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.*;

import server.CompanyInterface;
import server.EnterpriceInterface;
import server.InvestComInterface;
import server.LocationInterface;
import server.Location;
import server.PersonInterface;
import server.SubwayInterface;
import server.Subway;

import java.awt.BorderLayout;

public class Map extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private String name;
	public Person person=new Person();
	public static JLabel lblNewLabel;
	
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		try {
			Map dialog = new Map(1);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	
	public Map(int id) throws MalformedURLException, RemoteException, NotBoundException {
		setTitle("Welcome to Virtual City");
		setBounds(150, 10, 1024, 700);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(Color.WHITE);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		person.setPID(id);

		person.setCurrentLocationNum(2);

		
		//100, 100, 550, 350
		
		{
			
			final LocationButton button_com = new LocationButton("社区",0);
			//person.setCurrentLocationNum( button_com.getButtonNum() );
			button_com.setForeground(new Color(255, 255, 255));
			button_com.setFont(new Font("微软雅黑", Font.BOLD, 20));
			button_com.setBounds(202, 53,265,137);
			button_com.setToolTipText("点击进入社区！！！");
			button_com.setBorderPainted(false);
			button_com.setFocusPainted(false);
			button_com.setContentAreaFilled(false);
			button_com.setOpaque(false);
			button_com.setBackground(new Color(2,2,2));
			button_com.setBorderPainted(false);			
			//if( person.getCurrentLocationNum() == button_com.getButtonNum())
		    //	button_com.setEnabled(false);   
	        //else{		
			//button.setImage(new Image(display,"你的图片路径"));
			//ImageIcon  im=new ImageIcon("shequ.png");
			//JLabel jli=new JLabel(im);
			//if( person.getCurrentLocationNum() == button_com.getButtonNum())
			  //  button_com.setToolTipText("点击进入社区！！！");
			//else
				//button_com.setToolTipText("前往此地！！！");
			button_com.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				    if( person.getCurrentLocationNum() == button_com.getButtonNum()){
					
					
					    ComLoadGUI frame = new ComLoadGUI();
					    frame.setLocationRelativeTo(null);//****************************************
						frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						frame.setVisible(true);
				    }
			        else{
				       // button_com.setToolTipText("前往此地！！！");
				    //    button_com.addActionListener(new ActionListener() {
				//    public void actionPerformed(ActionEvent e) {
				    	SubEnter dialog = new SubEnter();
				    	
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);						
				    }
			    }
			});
			//	});
		//	}
			//ImageIcon im_0 = new ImageIcon("D:/eclipse/virtual city/VirtualCity/src/client/images/shequ.png");
			//JLabel  l1_1 = new JLabel(im_0);
		    //l1_1.setBounds(196, 151, 69, 186);
		    //contentPanel.add(l1_1);			
			contentPanel.setLayout(null);
			contentPanel.add(button_com);
		}
		
		final LocationButton button_res = new LocationButton("餐厅",1);
		button_res.setForeground(new Color(255, 255, 255));
		button_res.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_res.setToolTipText("点击进入餐厅！！！");
		button_res.setBounds(95, 282, 183, 95);
		button_res.setBorderPainted(false);
		button_res.setFocusPainted(false);
		button_res.setContentAreaFilled(false);
		button_res.setOpaque(false);
		button_res.setBackground(new Color(2,2,2));
		button_res.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_res.getButtonNum()){
			//button_res.setToolTipText("点击进入餐厅！！！");
	    button_res.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
		    	if( person.getCurrentLocationNum() == button_res.getButtonNum()){
			        ResEnterGUI res=new ResEnterGUI();
				    res.setLocationRelativeTo(null);//****************************************
				    res.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    res.setVisible(true);
					
			    }
		    	else{
		    		
				    SubEnter dialog = new SubEnter();
				    
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);	
		    	}
	        }
    	});
		contentPanel.add(button_res);
		
		final LocationButton button_bank = new LocationButton("银行",2);
		button_bank.setToolTipText("点击进入银行！！！");
		button_bank.setForeground(new Color(255, 255, 255));
		button_bank.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_bank.setBounds(363, 212, 78, 156);
		button_bank.setBorderPainted(false);
		button_bank.setFocusPainted(false);
		button_bank.setContentAreaFilled(false);
		button_bank.setOpaque(false);
		button_bank.setBackground(new Color(2,2,2));
		button_bank.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_bank.getButtonNum()){
			//button_bank.setToolTipText("点击进入银行！！！");
		button_bank.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_bank.getButtonNum()){
					HomePageGUI frame = new HomePageGUI();
					frame.setLocationRelativeTo(null);//****************************************
					frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					frame.setVisible(true);					
			    }
				else{
					SubEnter dialog = new SubEnter();
					
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);	
				}
			}
		});		
		contentPanel.add(button_bank);
		
		final LocationButton button_ent = new LocationButton("娱乐城",3);
		button_ent.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_ent.setForeground(new Color(255, 255, 255));
		button_ent.setToolTipText("点击进入娱乐城！！！");
		button_ent.setBounds(500, 399, 421, 229);
		button_ent.setBorderPainted(false);
		button_ent.setFocusPainted(false);
		button_ent.setContentAreaFilled(false);
		button_ent.setOpaque(false);
		button_ent.setBackground(new Color(2,2,2));
		button_ent.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_ent.getButtonNum()){
			//button_ent.setToolTipText("点击进入娱乐城！！！");
		button_ent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_ent.getButtonNum())
				{
					EntertainmentCity dialog = new EntertainmentCity("欢迎光临娱乐城！");
					dialog.setLocationRelativeTo(null);//****************************************
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
			    }
				else{
					SubEnter dialog = new SubEnter();
				
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);	
				}
			}
		});				
		contentPanel.add(button_ent);
		
		final LocationButton button_xiaoshou1 = new LocationButton("销售公司1",4);
		button_xiaoshou1.setForeground(new Color(255, 255, 255));
		button_xiaoshou1.setHorizontalAlignment(SwingConstants.LEFT);
		button_xiaoshou1.setFont(new Font("微软雅黑", Font.BOLD, 12));
		button_xiaoshou1.setToolTipText("点击进入销售公司1！！！");
		button_xiaoshou1.setBounds(494, 109, 104, 202);
		button_xiaoshou1.setBorderPainted(false);
		button_xiaoshou1.setFocusPainted(false);
		button_xiaoshou1.setContentAreaFilled(false);
		button_xiaoshou1.setOpaque(false);
		button_xiaoshou1.setBackground(new Color(2,2,2));
		button_xiaoshou1.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_xiaoshou1.getButtonNum()){
			//button_xiaoshou1.setToolTipText("点击进入销售公司1！！！");
		button_xiaoshou1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_xiaoshou1.getButtonNum())
				{
					EnterSaleCom_GUI salecom1 = new EnterSaleCom_GUI(5);
					salecom1.setLocationRelativeTo(null);//****************************************
					salecom1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					salecom1.setVisible(true);					
			    }
				else{
					SubEnter dialog = new SubEnter();
			
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);	
				}
			}
		});
		contentPanel.add(button_xiaoshou1);
		
		final LocationButton button_xiaoshou2 = new LocationButton("销售公司2",5);
		button_xiaoshou2.setForeground(new Color(255, 255, 255));
		button_xiaoshou2.setToolTipText("点击进入销售公司2！！！");
		button_xiaoshou2.setHorizontalAlignment(SwingConstants.LEFT);
		button_xiaoshou2.setFont(new Font("微软雅黑", Font.BOLD, 12));
		button_xiaoshou2.setBounds(629, 130, 94, 163);
		button_xiaoshou2.setBorderPainted(false);
		button_xiaoshou2.setFocusPainted(false);
		button_xiaoshou2.setContentAreaFilled(false);
		button_xiaoshou2.setOpaque(false);
		button_xiaoshou2.setBackground(new Color(2,2,2));
		button_xiaoshou2.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_xiaoshou2.getButtonNum()){
			//button_xiaoshou2.setToolTipText("点击进入销售公司2！！！");
		button_xiaoshou2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_xiaoshou2.getButtonNum())
				{
					EnterSaleCom_GUI salecom2 = new EnterSaleCom_GUI(6);
					salecom2.setLocationRelativeTo(null);//****************************************
					salecom2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					salecom2.setVisible(true);					
			    }
				else{
					SubEnter dialog = new SubEnter();
	
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);
				}
			}
		});
		contentPanel.add(button_xiaoshou2);
		
		final LocationButton button_touzi1 = new LocationButton("投资公司1",6);
		button_touzi1.setFont(new Font("微软雅黑", Font.BOLD, 12));
		button_touzi1.setForeground(new Color(255, 255, 255));
		button_touzi1.setToolTipText("点击进入投资公司2！！！");
		button_touzi1.setBounds(752, 168, 104, 200);
		button_touzi1.setBorderPainted(false);
		button_touzi1.setFocusPainted(false);
		button_touzi1.setContentAreaFilled(false);
		button_touzi1.setOpaque(false);
		button_touzi1.setBackground(new Color(2,2,2));
		button_touzi1.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_touzi1.getButtonNum()){
			//button_touzi1.setToolTipText("点击进入投资公司2！！！");
		button_touzi1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_touzi1.getButtonNum())
				{
					EnterInvestCom_GUI investcom1 = new EnterInvestCom_GUI(1);
					investcom1.setLocationRelativeTo(null);//****************************************
					investcom1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					investcom1.setVisible(true);					
			    }
				else{
					SubEnter dialog = new SubEnter();
			
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);
				}
			}
		});
		contentPanel.add(button_touzi1);
		
		final LocationButton button_touzi2 = new LocationButton("投资公司2",7);
		button_touzi2.setForeground(new Color(255, 255, 255));
		button_touzi2.setFont(new Font("微软雅黑", Font.BOLD, 12));
		button_touzi2.setToolTipText("点击进入投资公司2！！！");
		button_touzi2.setBounds(601, 10, 104, 166);
		button_touzi2.setBorderPainted(false);
		button_touzi2.setFocusPainted(false);
		button_touzi2.setContentAreaFilled(false);
		button_touzi2.setOpaque(false);
		button_touzi2.setBackground(new Color(2,2,2));
		button_touzi2.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_touzi2.getButtonNum()){
			//button_touzi2.setToolTipText("点击进入投资公司2！！！");
		button_touzi2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_touzi2.getButtonNum())
				{
					EnterInvestCom_GUI investcom2 = new EnterInvestCom_GUI(2);
					investcom2.setLocationRelativeTo(null);//****************************************
					investcom2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					investcom2.setVisible(true);					
			    }
				else{
					SubEnter dialog = new SubEnter();
			
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);		
				}
			}
		});
		contentPanel.add(button_touzi2);
		
		final LocationButton button_kaifa1 = new LocationButton("开发公司1",8);
		button_kaifa1.setForeground(new Color(255, 255, 255));
		button_kaifa1.setFont(new Font("微软雅黑", Font.BOLD, 12));
		button_kaifa1.setToolTipText("点击进入开发公司1！！！");
		button_kaifa1.setBounds(823, 28, 111, 130);
		button_kaifa1.setBorderPainted(false);
		button_kaifa1.setFocusPainted(false);
		button_kaifa1.setContentAreaFilled(false);
		button_kaifa1.setOpaque(false);
		button_kaifa1.setBackground(new Color(2,2,2));
		button_kaifa1.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_kaifa1.getButtonNum()){
			//button_kaifa1.setToolTipText("点击进入开发公司1！！！");
		button_kaifa1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_kaifa1.getButtonNum())
				{
					EnterDeveCom_GUI devecom1 = new EnterDeveCom_GUI(3);
					devecom1.setLocationRelativeTo(null);//****************************************
					devecom1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					devecom1.setVisible(true);				
			    }
				else{
					SubEnter dialog = new SubEnter();
		
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);		
				}
			}
		});
		contentPanel.add(button_kaifa1);
		
		final LocationButton button_kaifa2 = new LocationButton("开发公司2",9);
		button_kaifa2.setToolTipText("点击进入开发公司2！！！");
		button_kaifa2.setFont(new Font("微软雅黑", Font.BOLD, 12));
		button_kaifa2.setForeground(new Color(255, 255, 255));
		button_kaifa2.setBounds(847, 163, 94, 148);
		button_kaifa2.setBorderPainted(false);
		button_kaifa2.setFocusPainted(false);
		button_kaifa2.setContentAreaFilled(false);
		button_kaifa2.setOpaque(false);
		button_kaifa2.setBackground(new Color(2,2,2));
		button_kaifa2.setBorderPainted(false);
		//if( person.getCurrentLocationNum() == button_kaifa2.getButtonNum()){
			//button_kaifa2.setToolTipText("点击进入开发公司2！！！");
		button_kaifa2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if( person.getCurrentLocationNum() == button_kaifa2.getButtonNum())
				{
					EnterDeveCom_GUI devecom2 = new EnterDeveCom_GUI(4);
					devecom2.setLocationRelativeTo(null);//****************************************
					devecom2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					devecom2.setVisible(true);				
			    }
				else{
					SubEnter dialog = new SubEnter();
			
				    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				    dialog.setVisible(true);
				}
			}
		});
		contentPanel.add(button_kaifa2);
		
		JButton button_sub = new JButton("");//subway
		button_sub.setToolTipText("地铁");
		button_sub.setIcon(new ImageIcon("src/client/images/ditie.png"));
		button_sub.setBounds(0,0, 59, 58);
		button_sub.setBorderPainted(false);
		button_sub.setFocusPainted(false);
		button_sub.setContentAreaFilled(false);
		button_sub.setOpaque(false);
		button_sub.setBackground(new Color(2,2,2));
		button_sub.setBorderPainted(false);
		button_sub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SubEnter dialog = new SubEnter();
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		contentPanel.add(button_sub);
		
		JLabel label = new JLabel("当前位置：");
		label.setForeground(new Color(0, 255, 0));
		label.setBounds(56, 10, 116, 33);
		label.setFont(new Font("微软雅黑", Font.BOLD, 20));
		contentPanel.add(label);
		
		try{
		    Configuration config=new Configuration();
	        String rmiAddress=config.getRmiAddress();
	        LocationInterface currentLocationName = null;
	        currentLocationName=(LocationInterface) Naming.lookup(rmiAddress+"Location");
	        name = currentLocationName.getName( person.getCurrentLocationNum());
		 }catch(RemoteException e4){
		    	e4.printStackTrace();
		    } catch (MalformedURLException e5) {
		    	// TODO Auto-generated catch block
		    	e5.printStackTrace();
		    } catch (NotBoundException e6) {
		    	// TODO Auto-generated catch block
		    	e6.printStackTrace();
		    }
		lblNewLabel = new JLabel(name);
		lblNewLabel.setForeground(new Color(0, 255, 0));
		lblNewLabel.setBounds(158, 10, 182, 33);
		lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		contentPanel.add(lblNewLabel);
		
		
		Configuration config=new Configuration();
		String rmiAddress=config.getRmiAddress();
		
		final PersonInterface PI=(PersonInterface)  Naming.lookup(rmiAddress+"Person"); 
		Person per=new Person();
		final int pid=per.getPID();
		
		JLabel label_1 = new JLabel("姓名：");
		label_1.setBounds(22, 520, 54, 15);
		contentPanel.add(label_1);
		
		JLabel label_2 = new JLabel("性别：");
		label_2.setBounds(22, 570, 54, 15);
		contentPanel.add(label_2);
		
		JLabel label_3 = new JLabel("现金：");
		label_3.setBounds(22, 613, 54, 15);
		contentPanel.add(label_3);
		
		JLabel label_4 = new JLabel("工作：");
		label_4.setBounds(195, 520, 54, 15);
		contentPanel.add(label_4);
		
		final JLabel label_5 = new JLabel(""+PI.getName(pid));
		label_5.setBounds(86, 520, 99, 15);
		contentPanel.add(label_5);
		
		final JLabel label_6 = new JLabel(""+PI.getSex(pid));
		label_6.setBounds(86, 570, 99, 15);
		contentPanel.add(label_6);
		
		final JLabel label_7 = new JLabel(""+PI.getCash(pid));
		label_7.setBounds(86, 613, 99, 15);
		contentPanel.add(label_7);
		
		JLabel label_9 = new JLabel("体力值：");
		label_9.setBounds(195, 570, 54, 15);
		contentPanel.add(label_9);
		
		JLabel label_10 = new JLabel("愉悦值：");
		label_10.setBounds(195, 613, 54, 15);
		contentPanel.add(label_10);
		
		final JLabel label_12 = new JLabel(""+PI.getJob(pid));
		label_12.setBounds(254, 520, 131, 15);
		contentPanel.add(label_12);
		
		final JLabel label_13 = new JLabel(""+PI.getHp(pid));
		label_13.setBounds(254, 570, 131, 15);
		contentPanel.add(label_13);
		
		final JLabel label_14 = new JLabel(""+PI.getHap(pid));
		label_14.setBounds(254, 613, 131, 15);
		contentPanel.add(label_14);
		
		class RefreshThread extends Thread{ 

		    public void run() { 
		        while(true){		        	
		        	try {
		        		label_5.setText(""+PI.getName(pid));
		        		label_6.setText(""+PI.getSex(pid));
		        		label_7.setText(""+PI.getCash(pid));
		        		label_12.setText(""+PI.getJob(pid));
		        		label_13.setText(""+PI.getHp(pid));
		        		label_14.setText(""+PI.getHap(pid));
						sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		        }
		    } 

		}
		
		new RefreshThread().start();
		
		//D:/eclipse/virtual city/VirtualCity/
		ImageIcon im = new ImageIcon("src/client/images/city.png");
	    JLabel  l1 = new JLabel(im);
	    l1.setBounds(0, 0, 1024, 750);
	    contentPanel.add(l1);
	    
	}
}
